//
//  MovieView.m
//  WP_Movie
//
//  Created by GZCP1897 on 17/1/20.
//  Copyright © 2017年 GZCP1897. All rights reserved.
//

#import "MovieView.h"
#import <AVFoundation/AVFoundation.h>
#import <Masonry.h>
#import "VolumeView.h"
#import "LightView.h"
#import "TitleView.h"
#import "ProgressView.h"
#import "WpMovieConst.h"

#define Volume_LightViewScale 0.25
#define Volume_LightViewMinWidth 60
#define Volume_LightViewMaxWidth 120
#define Title_ProgressViewHeight 50

@interface MovieView ()
@property (nonatomic ,strong) AVPlayer *player;
@property (nonatomic ,strong) AVPlayerItem *playerItem;
@property(nonatomic, strong) AVPlayerLayer *playerlayer;

@property(nonatomic, assign)BOOL isFullScreen;//根据rate判断播放和暂停

@property(nonatomic, weak)UIImageView *playBtnImageV;

//回调
@property(nonatomic, copy)void(^fullScreenHandle)();
@property(nonatomic, copy)void(^deFullScreenHandle)();
@end

@implementation MovieView
#pragma mark - Public
- (instancetype)initWithFileUrl:(NSURL *)url fullScreen:(void (^)())fullScreenHandle deFullScreen:(void (^)())deFullScreenHandle{
    //创建AVPlayerItem
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:url];
    self.playerItem = item;
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayDidEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:item];// 添加视频播放结束通知
    
    //创建AVPlayer
    AVPlayer *player = [AVPlayer playerWithPlayerItem:item];
    self.player = player;
    
    //创建Playerlayer
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    self.playerlayer = playerLayer;

    
    self.fullScreenHandle = fullScreenHandle;
    self.deFullScreenHandle = deFullScreenHandle;
    return [self init];
}

#pragma mark - 事件监听
//视频播放结束
- (void)moviePlayDidEnd:(NSNotification *)notification {
    NSLog(@"Play end");
    self.playBtnImageV.hidden = NO;
}
//单击控制播放暂停
- (void)sigleTouchGesWork:(UITapGestureRecognizer *)tap{
    
    if (self.player.rate == 0) {//速率为0 暂停或还没开始播放
        
        NSLog(@"测试--fdjskla--%s", __func__);
        
        NSLog(@"开始播放");
        
        [self.player play];
        self.playBtnImageV.hidden = YES;
    }else{
        [self.player pause];
        self.playBtnImageV.hidden = NO;
    }
}
//双击控制全屏
- (void)doubleTouchGesWork:(UITapGestureRecognizer *)tap{
    
    if (self.isFullScreen) {
        self.deFullScreenHandle();
    }else{
        self.fullScreenHandle();
    }
    self.isFullScreen = !self.isFullScreen;
}


#pragma mark - Override
/**
 重写，进行布局控制 图层由下至上依次是
 2. 左右控制
 3. 下面控制
 
 */
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {

        //左侧声音控制 宽度为1/4视频宽度 占据左边1/4  先控制UI
        VolumeView *volumeV = [[VolumeView alloc] init];
        [self addSubview:volumeV];
    
        //右侧亮度控制
        LightView *lightV = [[LightView alloc] init];
        [self addSubview:lightV];
        
        //上面标题View
        TitleView *titleV = [[TitleView alloc] init];
        [self addSubview:titleV];
        
        //下面暂停与播放进度控制
        ProgressView *progressV = [[ProgressView alloc] init];
        [self addSubview:progressV];
        
        //暂停图片
        UIImageView *btnImageV = [[UIImageView alloc] init];
        self.playBtnImageV = btnImageV;
        UIImage *playImage = [UIImage imageNamed:@"video-play"];
        btnImageV.image = playImage;
        [self addSubview:btnImageV];
        
        [volumeV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(self);
            make.width.greaterThanOrEqualTo(@Volume_LightViewMinWidth).priorityHigh();
            make.width.lessThanOrEqualTo(@Volume_LightViewMaxWidth).priorityHigh();
            make.width.equalTo(self.mas_width).multipliedBy(Volume_LightViewScale).priorityLow();
        }];
        [lightV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.equalTo(self);
            make.width.greaterThanOrEqualTo(@Volume_LightViewMinWidth).priorityHigh();
            make.width.lessThanOrEqualTo(@Volume_LightViewMaxWidth).priorityHigh();
            make.width.equalTo(self.mas_width).multipliedBy(Volume_LightViewScale).priorityLow();
        }];
        [titleV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.equalTo(self);
            make.height.mas_equalTo(Title_ProgressViewHeight);
        }];
        [progressV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self);
            make.height.mas_equalTo(Title_ProgressViewHeight);
        }];
        [btnImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
            make.width.mas_equalTo(playImage.size.width);
            make.height.mas_equalTo(playImage.size.height);
        }];
        
        
    }
    return self;
}

- (instancetype)init{
    if (self = [super init]) {
        //一定要在这里添加  在前面添加self.layer不存在
//        [self.layer addSublayer:self.playerlayer];
        [self.layer insertSublayer:self.playerlayer atIndex:0];
        
        UITapGestureRecognizer * sigleTouchGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sigleTouchGesWork:)];
        sigleTouchGes.numberOfTapsRequired = 1;
        sigleTouchGes.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:sigleTouchGes];
        
        UITapGestureRecognizer * doubleTouchGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTouchGesWork:)];
        doubleTouchGes.numberOfTapsRequired = 2;
        doubleTouchGes.numberOfTouchesRequired = 1;
            [self addGestureRecognizer:doubleTouchGes];
        //共存问题
        [sigleTouchGes requireGestureRecognizerToFail:doubleTouchGes];
    }
    
    return self;
}

- (void)layoutSublayersOfLayer:(CALayer *)layer{
    [super layoutSublayersOfLayer:layer];//要super super super 重要的事情说三遍！！！
    self.playerlayer.anchorPoint = CGPointZero;
    self.playerlayer.bounds = layer.bounds;
    self.playerlayer.position = CGPointZero;
}


- (void)layoutSubviews{
    [super layoutSubviews];
    
    
}



- (void)dealloc{
    self.player = nil;
    self.playerItem = nil;
    self.playerlayer = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self]
    ;
    NSLog(@"测试--MovieView被销毁--%s", __func__);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
